package com.smb.app.android.stringstools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

public class StringsFileGenerator {

	public static void generatePropertiesStrings(List<KeyValueBean> list, String propertiesPath) {
		File file = new File(propertiesPath);
		if (file.exists()) {
			file.delete();
		}
		if (!file.exists()) {
			try {
				file.createNewFile();

				RandomAccessFile mm = null;

				FileOutputStream o = null;
				try {
					o = new FileOutputStream(propertiesPath);
					for (KeyValueBean keyValueBean : list) {
						String content = keyValueBean.getKey() + "=" + keyValueBean.getValue() + "\n";
						o.write(content.getBytes("UTF-8"));
					}

					o.close();
					// mm=new RandomAccessFile(fileName,"rw");
					// mm.writeBytes(content);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				} finally {
					if (mm != null) {
						mm.close();
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public static void generateStringsXML(List<KeyValueBean> list, String xmlPath) {

		// 生成XML文件,1、创建一个SAXTransformerFactory的对象
		SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
		// 2、通过SAXtransformerFactory对象创建一个TransformerHandler的对象
		TransformerHandler handler = null;
		try {
			handler = tff.newTransformerHandler();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 3、通过Handler对象创建一个Transformer对象
		Transformer tr = handler.getTransformer();
		// 4、通过Transformer对象对生成的XML文件进行设置
		tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");
		tr.setOutputProperty(OutputKeys.INDENT, "yes");
		// 5、创建一个result对象,并与Handler关联
		File file = new File(xmlPath);
		file.mkdir();
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		Result result = null;
		try {
			result = new StreamResult(new FileOutputStream(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		handler.setResult(result);

		// 利用Handler对象进行XML文件内容的编写
		// 打开document
		try {
			handler.startDocument();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AttributesImpl atts = new AttributesImpl();
		try {
			handler.startElement("", "", "resources", atts);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (KeyValueBean keyValueBean : list) {
			atts.clear();
			try {
				atts.addAttribute("", "", "name", "", keyValueBean.getKey());
				handler.startElement("", "", "string", atts);
				handler.characters(keyValueBean.getValue().toCharArray(), 0,
						keyValueBean.getValue().toCharArray().length);

				handler.endElement("", "", "string");
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {
			handler.endElement("", "", "resources");
			handler.endDocument(); // 关闭document
		} catch (SAXException e) {
			e.printStackTrace();
		}

	}

}
